﻿using System.Collections.Generic;
using System.Linq;
using Tau.DataAccess;

namespace Tau.Service.Adapters
{
    internal class Product : IAdapter<Model.Product, tblItem>
    {
        public Model.Product GetModelObject(tblItem dbObject)
        {
            var model = new Model.Product()
                                  {
                                      Description = dbObject.itemDesc,
                                      Hide = dbObject.hide.GetValueOrDefault(),
                                      Image = dbObject.itemImg,
                                      ImageId = dbObject.imageID.GetValueOrDefault(),
                                      LargeImage = dbObject.itemImgLg,
                                      Price = dbObject.price.GetValueOrDefault(),
                                      ProductId = dbObject.itemID,
                                      ProductName = dbObject.itemName,
                                      ProductNumber = dbObject.itemNumber,
                                      Shipping2 = dbObject.shipping2.GetValueOrDefault(),
                                      ShippingCost = dbObject.shippingCost.GetValueOrDefault(),
                                      ShortDescription = dbObject.itemShortDesc,
                                      Special = dbObject.special.GetValueOrDefault(),
                                      Spotlight = dbObject.spotlight.GetValueOrDefault(),
                                      WholesalePrice = dbObject.wholesalePrice.GetValueOrDefault()
                                  };
            return model;
        }

        public tblItem GetDbObject(Model.Product model)
        {
            var dbObject = new tblItem()
                           {
                               hide = model.Hide,
                               itemDesc = model.Description,
                               itemID = model.ProductId,
                               itemImg = model.Image,
                               itemImgLg = model.LargeImage,
                               itemName = model.ProductName,
                               itemNumber = model.ProductNumber,
                               itemShortDesc = model.ShortDescription,
                               price = model.Price,
                               shipping2 = model.Shipping2,
                               shippingCost = model.ShippingCost,
                               special = model.Special,
                               spotlight = model.Spotlight,
                               wholesalePrice = model.WholesalePrice
                           };
            if (model.ImageId > 0)
            {
                dbObject.imageID = model.ImageId;
            }
            return dbObject;
        }

        public List<Model.Product> GetModelList(List<tblItem> dbObjectList)
        {
            return dbObjectList.Select(dbProduct => GetModelObject(dbProduct)).ToList();
        }
    }
}
